package com.cgnpc.scp.manager.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cgnpc.cuddemo.utils.IDHelper;
import com.cgnpc.framework.userinfo.CurrentUser;
import com.cgnpc.scp.common.bean.MyPageResult;
import com.cgnpc.scp.common.pojo.UserInfo;
import com.cgnpc.scp.common.utils.MyPageResultUtil;
import com.cgnpc.scp.common.utils.StringUtil;
import com.cgnpc.scp.manager.mybatis.entity.*;
import com.cgnpc.scp.manager.mybatis.mapper.client.DebugtipsMapper;
import com.cgnpc.scp.manager.mybatis.mapper.client.SystemconfigMapper;
import com.cgnpc.scp.manager.mybatis.mapper.client.SysteminfoMapper;
import com.cgnpc.scp.manager.service.DebugTipService;
import com.cgnpc.scp.manager.service.SystemConfigService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SystemConfigServiceImpl implements SystemConfigService {
    private static final Logger LOGGER = LoggerFactory.getLogger(SystemConfigServiceImpl.class);

    @Autowired
    private SystemconfigMapper systemconfigMapper;
    @Autowired
    private CurrentUser currentUser;

    @Autowired
    private SysteminfoMapper systeminfoMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int saveOrUpdate(List<Systemconfig> systemconfigList) throws Exception {
        UserInfo loginUserInfo = currentUser.getCurrentUserDetail();
        String loginUserInfoUserId = loginUserInfo.getUserId();
        String loginUserInfoUserName = loginUserInfo.getUserName();
        QueryWrapper<Systemconfig> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("create_user",loginUserInfoUserId );
        systemconfigMapper.delete(queryWrapper);
        for (Systemconfig systemconfig:systemconfigList){
            systemconfig.setId(IDHelper.uuid());
            systemconfig.setCreateUser(loginUserInfoUserId);
            systemconfig.setCreateUserName(loginUserInfoUserName);
            systemconfig.setCreateTime(new Date());
            systemconfig.setLastUpdateUser(loginUserInfoUserId);
            systemconfig.setLastUpdateUserName(loginUserInfoUserName);
            systemconfig.setLastUpdateTime(new Date());
            systemconfigMapper.insert(systemconfig);
        }
        return 1;
//        if (StringUtil.isNullOrSpace(systemconfig.getId())) {
//        } else {
//            SystemconfigExample systemconfigExample = new SystemconfigExample();
//            SystemconfigExample.Criteria systemconfigExampleCriteria = systemconfigExample.createCriteria();
//            systemconfigExampleCriteria.andSystemInfoIdEqualTo(systemconfig.getSystemInfoId());
//            updateRows = systemconfigMapper.updateByExampleSelective(systemconfig, systemconfigExample);
//        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int del(List<String> ids) throws Exception {
        int updateRows = 0;
        QueryWrapper<Systemconfig> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("system_info_id",ids);
//        systemconfigExampleCriteria.andCreateUserEqualTo("");
        updateRows = systemconfigMapper.delete(queryWrapper);
        return updateRows;
    }

    @Override
    public List<Systemconfig> loadByPaging () throws Exception {
        // mybatis查询方式
        UserInfo loginUserInfo = currentUser.getCurrentUserDetail();
        QueryWrapper<Systemconfig> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("order_index") ;
        queryWrapper.eq("create_user",loginUserInfo.getUserId());
        // 执行查询
        List<Systemconfig> systemconfigs = systemconfigMapper.selectList(queryWrapper);
        if(systemconfigs!=null&& systemconfigs.size()>0){
           List<String> sysInfoIDs=systemconfigs.stream().map(p -> p.getSystemInfoId()).collect(Collectors.toList());
            List<Systeminfo>systeminfoList=systeminfoMapper.selectBatchIds(sysInfoIDs);
            for(Systeminfo systeminfo:systeminfoList){
                for(Systemconfig systemconfig:systemconfigs){
                    if(systemconfig.getSystemInfoId().equals(systeminfo.getId())){
                        systemconfig.setSystemName(systeminfo.getSystemName());
                        systemconfig.setSystemUrl(systeminfo.getSystemUrl());
                        systemconfig.setImageCode(systeminfo.getImageCode());
                    }
                }
            }
        }
        return systemconfigs;
    }
}